<template>
	<scroll-view class="list-scroll" scroll-y @scrolltolower="getList()" @refresherrefresh="getList(true)"
		:refresher-enabled="true" :refresher-triggered="refresherTriggered">
		<view class="list-box">
			<view v-for="(item,index) in list" :key="index" @click="itemClick(item)">
				<view class="item_view">
					<view class="item_top">
						<view class="left_title">
							<image class="icon_img" src="/static/image/xrw.png"></image>
							<text class="text_title">{{item.msgTitle}}</text>
						</view>
						<text class="text_time">{{item.createTime&&item.createTime.replace("T",' ')}}</text>
					</view>
					<text style="margin-top: 20rpx;">{{item.msgContent}}</text>
				</view>
			</view>
			<view class="get-more" v-show="getMoreStatus">
				<text>正在加载更多。。。</text>
			</view>
			<empty-waybill v-show="list.length==0"></empty-waybill>
		</view>
	</scroll-view>
</template>

<script>
	import emptyWaybill from "@/components/tool-empty/waybill";
	export default {
		components: {
			emptyWaybill
		},
		data() {
			return {
				msgType: "",
				userRoleType: "",
				refresherTriggered: false,
				getMoreStatus: false,
				list: [],
				pages: {
					size: 10, // 每次加载的数量
					current: 1, // 记录当前页
					total: '', // 记录总数
				}
			};
		},
		computed: {
			refuseMore() {
				// 计算是否还有更多
				return this.pages.total === this.list.length;
			}
		},
		methods: {
			itemClick(item) {
				this.changeReadType(item).then(() => {
					if (this.msgType == '1' && this.userRoleType !== 2 && item.readType == '0') {
						this.$api.busidispcheckDetail({
							'taskId': item.note
						}).then((res) => {
							const resData = res.data.data;
							// 出车前检查
							this.$goBack('/pages/dgs_qy/waybill/inspect/out-car?param=' + JSON.stringify({
								'taskId': item['note'],
								'vehCode': resData['vehCode'],
								'trailerVehCode': resData['trailerVehCode'],
								'goodsInfo': '',
								'logType': '',
								'vehId': resData['vehId'],
								'trailerId': resData['trailerId']
							}))
						})
					} else if (this.msgType == '2' && this.userRoleType !== 2 && item.readType == "0") {
						this.$goBack('/pages/dgs_qy/index/index?pageIndex=1');
					} else if (this.msgType == '6') {
						var msgContent = item.msgContent;
						if (msgContent.indexOf('活动台账管理') !== -1) {
							this.$goBack('/pages/dgs_qy/standingBook/list');
						} else if (msgContent.indexOf('作废待审核运单') !== -1) {
							this.$goBack('/pages/dgs_qy/waybill/list');
						} else if (msgContent.indexOf('安全检查管理') !== -1) {
							this.$goBack('/pages/dgs_qy/task/qy_ts_mg?pageIndex=2');
						}
					}
				});
			},
			changeReadType(item) {
				return this.$api.mgntcomsglogUpdate({
					targetId: item.targetId,
					comMsgId: item.comMsgId,
				});
			},
			// 加载，刷新
			getList(isRefresh) {
				// console.log(isRefresh)
				if (isRefresh) {
					this.list = [];
					this.refresherTriggered = true;
					this.pages.current = 1;
					this.pages.total = '';
				} else {
					if (this.refuseMore) {
						return false;
					}
					this.getMoreStatus = true;
					this.pages.current++;
				}
				// console.log(this.$store)
				const formData = {
					targetId: this.$store.state.userInfo.empInfoId,
					msgType: this.msgType,
					...this.pages
				};
				if (this.msgType == 6 && this.userRoleType == 2) {
					formData.dealType = 0;
				}
				this.$api.mgntcomsglogPage(formData).then(res => {
					// console.log(res)
					const resData = res.data.data;
					// console.log(resData)
					if (isRefresh) {
						this.list = resData.records;
					} else {
						this.list = this.list.concat(resData.records);
					}
					this.pages.total = resData.total;
					this.refresherTriggered = false;
					this.getMoreStatus = false
				})
			},
		},
		onLoad(option) {
			const param = option.param ? JSON.parse(option.param) : {};
			if (!param.msgType) {
				this.$goBack('./notice', 5);
				return false;
			}
			this.msgType = param.msgType;
			this.userRoleType = this.$store.state.userInfo.userRoleType;
			uni.setNavigationBarTitle({
				title: param.title
			});
			this.getList(true)
		}
	}
</script>

<style lang="scss">
	.list-scroll {
		height: calc(100vh - 5rpx);
		box-sizing: border-box;
	}

	.list-box {
		padding-bottom: 84rpx;
		position: relative;

		.get-more {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			text-align: center;
			line-height: 3em;
			font-size: 28rpx;
			color: #666666;
		}

	}

	.item_view {
		background-color: #FFFFFF;
		border-radius: 20rpx;
		padding: 20rpx 30rpx;
		display: flex;
		flex-direction: column;
		margin: 20rpx 20rpx 0rpx 20rpx;

		.item_top {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left_title {
				display: flex;
				align-items: center;

				.icon_img {
					width: 40rpx;
					height: 50rpx;
				}

				.text_title {
					font-size: 32rpx;
					font-weight: 700;
					margin-left: 25rpx;
				}
			}

			.text_time {
				color: #9C9C9C;
				font-size: 28rpx;
			}
		}
	}
</style>
